//
//  AppreciationDetailViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppreciationDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "AppreciationDetailView.h"
@interface AppreciationDetailViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)NSMutableArray *photoArr;
@property (nonatomic, copy) NSString *sandBoxPath;
@property (nonatomic, retain) NSMutableArray *arr2;


@end

@implementation AppreciationDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photoArr = [NSMutableArray array];
        self.sandBoxPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"appreclation.plist"];
        self.arr2 = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:self.sandBoxPath]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    AppreciationDetailView *detailView = [[AppreciationDetailView alloc]initWithFrame:self.view.frame];
    detailView.model = self.mobel;

    NSArray *arr = self.mobel.photos;
    for (NSDictionary *dic in arr) {
        NSString *strUrl = [dic objectForKey:@"url"];
        [self.photoArr addObject:[NSURL URLWithString:strUrl]];
    }
    
    detailView.photoArr = self.photoArr;
    detailView.titleLabel.text = self.mobel.title;
    detailView.detailLabel.text = self.mobel.text;

    [self.view addSubview:detailView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(seve)];
    
}

- (void)seve
{
    NSMutableArray *arr1 = [NSKeyedUnarchiver unarchiveObjectWithFile:self.sandBoxPath];
    BOOL epule = NO;
    
    for (AppreciationModel *model in arr1) {
        if ([model.title isEqualToString: _mobel.title]) {
            epule = YES;
        }
    }
    
    if (epule == YES) {
        UIAlertView *recur = [[UIAlertView alloc]initWithTitle:@"已经收藏过了" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [recur show];
    }else{
        [self.arr2 addObject:self.mobel];
        UIAlertView *collerction = [[UIAlertView alloc]initWithTitle:@"已收藏" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [collerction show];
        [NSKeyedArchiver archiveRootObject:self.arr2 toFile:self.sandBoxPath];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_photoArr release];
    [_mobel release];
    [_sandBoxPath release];
    [_arr2 release];
    [super dealloc];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
